
* === Robustness: Changes in treatment effect when varying minimum work distance

/* This dataset is made in ${dofiles}05b_project_specific_data_incl_short_wd.do */
use ${newdata}carownership_dataset_bergen_shortwd.dta, clear

/* Only keep required obs */
keep if $trmgroup != 0 
keep if $trmgroup != .
keep if 	(year >= $firstyearpre  & year <= $lastyearpre) ///
		  | (year >= $firstyearpost & year <= $lastyearpost)

/* setup variables */
capt drop bergen 		  
gen commuters = ($trmgroup == 1 | $trmgroup == 3) 
gen bergen = 	($trmgroup == 1 | $trmgroup == 2)
gen post = (year >= $firstyearpost & year <= $lastyearpost)
gen bergen_commuters = bergen * commuters
gen commuters_post = commuters * post
gen bergen_commuters_post = bergen_commuters * post

/* All time-varying variables based on geography (residence or work location)
   are set to their 2014 values (variation post 2014 is endogenous to treatment) */
gen dist_orig = dist // storing original work distance   
foreach var in dist time_work PublicVSCarTime_fam_mean PublicDiffCarTime_fam_mean grk {
	replace `var' = . if year != 2014
	bysort familienr (`var'): replace `var' = `var'[_n-1] if missing(`var')
}

sort familienr year
egen long famid_num = group(familienr)

/* === Run regressions ====================================================== */

eststo clear

forvalues i = 1/4 {
	local kmthreshold = `i' + 1
	drop if dist < `kmthreshold' | dist_orig < `kmthreshold' 
	
	reghdfe bev ///
		bergen_commuters_post /// DiDiD: post commuter in bergen	
		$xvar ///
		i.commuters#i.year /// 
		bergen_commuters /// 
		, absorb( ///
			i.grk#i.year ///
			i.famid_num ///
		) vce(cluster grk)	
	
	eststo reg_`i'
	summarize bev if e(sample) == 1 & year == $lastyearpre & bergen_commuters == 1
	estadd scalar MeanValue1 = r(mean), replace
	summarize bev if e(sample) == 1 & year == $lastyearpost & bergen_commuters == 1
	estadd scalar MeanValue2 = r(mean), replace
	eststo reg_`i' 
	* Saving estimates to ster files	
	estimates save ${ster}TableD1_col`i', replace	
}

esttab, keep(bergen_commuters_post)	

/* === Reading regression results from ster files, saving to .tex file ====== */
eststo drop *

forvalues i = 1/4 {
	estimates use "${ster}TableD1_col`i'" 
	eststo
}
esttab * ///
	using "${tables}tabD1est.tex", ///
	keep(bergen_commuters_post)  ///
	coeflabels( ///
	bergen_commuters_post "Post $\times$ Paying commuters $\times$ Bergen" ///
	) ///
	fragment nodepvars nonumbers nogaps nolines nonotes noobs ///
	nomtitle /// 
	prehead("") posthead("") ///
	stats() ///
	se  ///
	star(* 0.10 ** 0.05 *** 0.01)  ///
	replace	substitute(\_ _)

esttab * ///
	using "${tables}tabD1sumstat.tex", ///
	drop(*)  ///
	fragment nodepvars nonumbers nogaps nolines nonotes noobs ///
	nomtitle  /// 
	prehead("") posthead("") ///
	stats(N  MeanValue1 MeanValue2, ///
	label("Observations"  "Mean depvar 2014 (paying commuters, Bergen)" "Mean depvar 2017 (paying commuters, Bergen)" ///
	) fmt(0 4 4) ) ///
	se  ///
	star(* 0.10 ** 0.05 *** 0.01)  ///
	replace	substitute(\_ _)